import { ApiProperty } from '@nestjs/swagger';
import { IsNotEmpty, IsString, IsArray, ValidateNested, IsNumber, IsOptional, Min, ArrayMinSize } from 'class-validator';
import { Type } from 'class-transformer';

export class CreateOrderItemDto {
  @ApiProperty({ description: '图书ID', example: 'uuid' })
  @IsNotEmpty({ message: '图书ID不能为空' })
  @IsString({ message: '图书ID必须是字符串' })
  book_id: string;

  @ApiProperty({ description: '数量', example: 1 })
  @IsNotEmpty({ message: '数量不能为空' })
  @IsNumber({}, { message: '数量必须是数字' })
  @Min(1, { message: '数量最小为1' })
  quantity: number;
}

export class CreateOrderDto {
  @ApiProperty({ description: '收货地址', example: '北京市海淀区中关村大街1号' })
  @IsNotEmpty({ message: '收货地址不能为空' })
  @IsString({ message: '收货地址必须是字符串' })
  shipping_address: string;

  @ApiProperty({ description: '联系电话', example: '13800138000' })
  @IsNotEmpty({ message: '联系电话不能为空' })
  @IsString({ message: '联系电话必须是字符串' })
  phone: string;

  @ApiProperty({ description: '备注', example: '请尽快发货', required: false })
  @IsOptional()
  @IsString({ message: '备注必须是字符串' })
  note?: string;

  @ApiProperty({ description: '订单项', type: [CreateOrderItemDto] })
  @IsArray({ message: '订单项必须是数组' })
  @ArrayMinSize(1, { message: '订单项不能为空' })
  @ValidateNested({ each: true })
  @Type(() => CreateOrderItemDto)
  items: CreateOrderItemDto[];
}